<!--
  功能：试卷管理
  作者：licy
  邮箱：licy@briup.com
  时间：2021年01月13日 14:14:54
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='question_bank_list'>
    <!-- 按钮区域 -->
    <div class="btns">
      <el-row>
        <el-col :span="16">
          <el-form inline size="small">
            <el-form-item label="名称">
              <el-input v-model="param.name" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="loadPapers">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="8" style="text-align:right">
          <el-button type="primary" size="small" @click="toSaveHandler">创 建 试 卷</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 按钮区域结束 -->
    <!-- 表格 -->
    <el-table size="small" :data="paperData.list" v-loading="loading">
      <el-table-column label="序号" type="index" :index="1" fixed="left"></el-table-column>
      <el-table-column label="名称" prop="name" min-width="340" fixed="left"> </el-table-column>
      <el-table-column label="状态" prop="status" align="center" width="100"> </el-table-column>
      <el-table-column label="总分" prop="scoreTotal" width="80" align="center"> </el-table-column>
      <el-table-column label="题量" prop="numTotal" width="80" align="center"> </el-table-column>
      <el-table-column label="单选题" prop="numRadio" width="80" align="center"> </el-table-column>
      <el-table-column label="多选题" prop="numCheck" width="80" align="center"> </el-table-column>
      <el-table-column label="判断题" prop="numJudge" width="80" align="center"> </el-table-column>
      <el-table-column label="填空题" prop="numCompletion" width="80" align="center"> </el-table-column>
      <el-table-column label="简答题" prop="numAnswer" width="80" align="center"> </el-table-column>
      <el-table-column  label="创建时间" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.createTime | fmtDate}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" :disabled='scope.row.status != "未组卷"' @click="toMergeHandler(scope.row)">组卷</el-button>
          <el-button type="text" size="mini" :disabled='scope.row.status != "已完成"' @click="toDetailsHandler(scope.row)">预览</el-button>
          <el-button type="text" size="mini" :disabled='scope.row.status != "已完成"' @click="toExportHandler(scope.row)">导出</el-button>
          <el-button type="text" size="mini" :disabled='scope.row.status != "已完成"' @click="toEditHandler(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="toDeleteHandler(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 分页 -->
   <div class="page">
      <el-pagination background 
        layout="prev, pager, next,sizes" 
        small
        :total="paperData.total" 
        :page-size="paperData.pageSize" 
        :current-page="paperData.page"
        :page-sizes="[5, 10, 15, 20]"
        @size-change="handleSizeChange"
        @current-change="currentChangeHandler">
      </el-pagination>
   </div>
    <!-- 分页 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="40%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form :model="form" ref="paperForm" :rules="rules" label-width="80px" size="mini">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitHandler">确定</el-button>
      </span>
    </Briupdrawer>
    <!-- 抽屉结束 -->
  </div>
</template>

<script>
import {download, downloadWord, get,post} from '@/utils/request'
import _ from 'lodash'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
    visible:false,
    loading:false,
    title:"",
    param:{
      page:1,
      pageSize:10
    },
    paperData:{
      list:[]
    },
    fileList:[],
    form:{},
    rules:{
      name: [ { required: true, message: '请输入名称', trigger: 'blur' } ],
    }
   }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 导出
    toExportHandler(row){
      let url = "/exam/paper/exportPaper"
      downloadWord(url,{id:row.id},row.name);
    },
    // 跳转到试卷详情
    toDetailsHandler(paper){
      this.$router.push({
        path:"/sms/professor/exam/PaperDetails",
        query:paper
      })
    },
    // 上传成功
    uploadSuccessHandler(response){
      if(response.status == 200){
        this.$set(this.form,'photo',response.data.id);
      } else {
        this.$message({type:'error',message:'附件服务器异常！'})
        this.visible = false;
      }
    },
    // 确认提交处理函数
    submitHandler(){
      // 表单校验
      this.$refs['paperForm'].validate((valid) => {
        if (valid) {
          // 校验通过后，完成数据交互
          let url = '/exam/paper/saveOrUpdate'
          post(url,this.form).then(response => {
            this.$message({type:"success",message:response.message})
            this.loadPapers();
            this.visible = false;
          })
        } else {
          return false;
        }
      });
    },
    // 去保存
    toSaveHandler(){
      this.title = "新增试卷信息";
      this.form = {};
      this.fileList = [];
      this.visible = true;
    },
    // 合并试卷
    toMergeHandler(row){
      // 将当前行数据的克隆对象赋值给当前form,这样可以避免修改表单数据导致表格数据发生变化。
      this.$router.push({
        path:"/sms/professor/exam/PaperMerge",
        query:row
      })
    },
    // 去修改
    toEditHandler(row){
      // 将当前行数据的克隆对象赋值给当前form,这样可以避免修改表单数据导致表格数据发生变化。
      this.$router.push({
        path:"/sms/professor/exam/PaperEdit",
        query:row
      })
    },
    // 去删除
    toDeleteHandler(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = '/exam/paper/deleteById'
        get(url,{id:row.id}).then(response =>{
          this.$message({ type: 'success', message: '删除成功!' });
          this.loadPapers();
        })
      })
    },
    // 查询试卷信息
    loadPapers(){
      // 添加loading,页面加载开始
      this.loading = true;
      let url = "/exam/paper/pageQuery"
      get(url,this.param).then(response => {
        this.paperData = response.data;
        // 页面加载结束
        this.loading = false;
      })
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadPapers();
    },
    // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadPapers();
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['paperForm'].clearValidate();
    },
    // 处理visible变化的钩子函数，解决抽屉无法收缩的bug
    handlerVisibleChange(val) {
      this.visible = val
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // 加载试卷信息
    this.loadPapers();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>

</style>
